package ezy.boost.update;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;



import cn.pedant.SweetAlert.SweetAlertDialog;
import txgw.feihong.com.txgwlibrary.R;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName UpdateProgressDialog
 * @package com.feihong.txgw.view
 * @data 2018/4/20 10:58
 * @Describe 重新定义更新progressDialog
 * @Change
 */
public class UpdateProgressDialog {
    private String mDialogTitle;
    private int mAllProgress, mSecondProgress;
    private Context mContext;
    private View mView;
    private SweetAlertDialog mDialog;
    private UpdateHandler mHandler;

    private TextView mUpdateDialogTitle;
    private ProgressBar mUpdateProgressBar;
    private TextView mUpdateDialogAllProgress;
    private TextView mUpdateDialogProgress;

    public UpdateProgressDialog(Context context) {
        mContext = context;
        mView = LayoutInflater.from(mContext).inflate(R.layout.update_dialog_layout, null, false);
        initView();
    }

    private void initView() {

        mUpdateDialogTitle = mView.findViewById(R.id.update_dialog_title_tv);
        mUpdateProgressBar = mView.findViewById(R.id.update_dialog_progress);
        mUpdateDialogProgress = mView.findViewById(R.id.update_dialog_progress_tv);
        mUpdateDialogAllProgress = mView.findViewById(R.id.update_dialog_allprogress_tv);


        mHandler = new UpdateHandler();
        mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.NORMAL_TYPE);
        mDialog.setCustomView(mView);
        mDialog.setCancelable(false);
        mDialog.showCancelButton(false);
        mDialog.showContentText(false);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setTitle("");
        mDialog.setContentText("");
    }

    public void show() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        mDialog.show();
        if (mDialog.getButton(SweetAlertDialog.BUTTON_CONFIRM) != null) {
            mDialog.getButton(SweetAlertDialog.BUTTON_CONFIRM).setVisibility(View.GONE);
        }

        if (mDialog.getButton(SweetAlertDialog.BUTTON_CANCEL) != null) {
            mDialog.getButton(SweetAlertDialog.BUTTON_CANCEL).setVisibility(View.GONE);
        }
    }

    public void dismissDialog(){
        if(mDialog != null && mDialog.isShowing()){
            mDialog.dismiss();
        }
    }
    public void setSeondProgress(int progress) {
//        mSecondProgress = progress;
        mUpdateProgressBar.setSecondaryProgress(progress);
        if (progress > 100) {
            mUpdateDialogProgress.setText("100%");
            mUpdateDialogAllProgress.setText("100/100");
        } else {
            mUpdateDialogProgress.setText(progress + "%");
            mUpdateDialogAllProgress.setText(progress + "/100");
        }
//        mHandler.sendEmptyMessage(0);
    }

    class UpdateHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
                    mUpdateProgressBar.setSecondaryProgress(mSecondProgress);
        if (mSecondProgress > 100) {
            mUpdateDialogProgress.setText("100%");
            mUpdateDialogAllProgress.setText("100/100");
        } else {
            mUpdateDialogProgress.setText(mSecondProgress + "%");
            mUpdateDialogAllProgress.setText(mSecondProgress + "/100");
        }
        }
    }
}
